﻿using System;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.UI;

namespace Editor
{
    [Serializable]
    public class Layer
    {
        public int[] data;
        public string name;
        public string type;
        public int height;
        public int width;
    }
    [Serializable]
    public class Tiled
    {
        public int height;
        public int width;
        public int tileheight;
        public int tilewidth;
        public Layer[] layers;
    }
    
    public static class ExportNaviMesh
    {
        [MenuItem("Tools/NaviMesh/Export")]
        private static void CreateRedBlueGameObject()
        {
            NavMeshTriangulation navtri = NavMesh.CalculateTriangulation();
            Debug.Log(Path.GetDirectoryName(exportPath));
            string jsonName = Path.Combine(Path.GetDirectoryName(exportPath),
                Path.GetFileNameWithoutExtension(exportPath) + ".navmesh.json");
            Debug.Log("三角形数:"+navtri.areas.Length);
            if (!Directory.Exists(Path.GetDirectoryName(jsonName)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(jsonName) ?? string.Empty);
            }
            File.WriteAllText(jsonName, JsonUtility.ToJson(navtri));
        }

        private static string exportPath = Path.Combine(Application.temporaryCachePath,"out");

        [MenuItem("Tools/NaviMesh/Load Tiled Map")]
        private static void LoadTiledMap()
        {
            ExportNaviMesh.exportPath = EditorUtility.OpenFilePanel("select path", "", "");
            Tiled tiled = JsonUtility.FromJson<Tiled>(File.ReadAllText(ExportNaviMesh.exportPath));
            Debug.Log(tiled);
            foreach (var layer in tiled.layers)
            {
                if (layer.type == "tilelayer" && layer.name == "grid")
                {
                    // GameObject plane= GameObject.CreatePrimitive(PrimitiveType.Plane);
                    // plane.name = Path.GetFileNameWithoutExtension(exportPath);
                    // Debug.Log(plane.GetComponent<Collider>().bounds);
                    // plane.isStatic = true;
                    GameObject mapGO = new GameObject(Path.GetFileNameWithoutExtension(exportPath));
                    GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane);
                    plane.isStatic = true;
                    plane.transform.parent = mapGO.transform;
                    plane.transform.localScale = new Vector3((float)layer.width * 3.2f, 1, (float)layer.height * 1.6f);
                    plane.transform.position = new Vector3((float)layer.width * 32f/2, 0.5f, (float)layer.height * 16f/2);
                    GameObject blockRoot = new GameObject("block");
                    blockRoot.transform.parent = mapGO.transform;
                    blockRoot.AddComponent<MeshRenderer>();
                    blockRoot.isStatic = true;
                        
                    
                    for (int y = 0; y < layer.height; y++)
                    {
                        for (int x = 0; x < layer.width; x++)
                        {
                            if (layer.data[x + y * layer.width] == 1)
                            {
                                GameObject block = GameObject.CreatePrimitive(PrimitiveType.Cube);
                                block.transform.localScale = new Vector3(32, 40, 16);
                                block.isStatic = true;
                                block.transform.parent = blockRoot.transform;
                                block.transform.position = new Vector3(x*32+16, 20, y*16+8);
                                block.AddComponent<NavMeshObstacle>();
                            }
                        }
                    }
                }
            }
        }


        private static string JsonPath(string fileName)
        {
            return Path.Combine(Application.streamingAssetsPath, $"{fileName}.json");
        }
    }
}